/**    
* @Title: RightsServiceImpl.java  
* @Package com.kaver.sso.service.impl   
* @author roc   
* @date Aug 9, 2021 2:39:28 PM  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.sso.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaver.sso.dao.po.SsoRights;
import com.kaver.sso.dao.po.SsoRoleRights;
import com.kaver.sso.dao.service.SsoRightsDao;
import com.kaver.sso.dao.service.SsoRoleRightsDao;
import com.kaver.sso.def.BizException;
import com.kaver.sso.def.dto.RightsDto;
import com.kaver.sso.def.dto.RoleDto;
import com.kaver.sso.def.enums.CodeEnum;
import com.kaver.sso.def.req.AddRightsReq;
import com.kaver.sso.def.req.DelRightsReq;
import com.kaver.sso.def.req.GroupRightsReq;
import com.kaver.sso.def.req.HasRightsReq;
import com.kaver.sso.def.req.RoleRightsReq;
import com.kaver.sso.def.req.SetRightsReq;
import com.kaver.sso.def.req.UpdateRightsReq;
import com.kaver.sso.def.req.UserRightsReq;
import com.kaver.sso.helper.SsoUserHolder;
import com.kaver.sso.service.RightsService;
import com.kaver.sso.service.RoleService;

/**  
* @ClassName: RightsServiceImpl  
* @Description: 权限
* @author roc  
* @date Aug 9, 2021 2:39:28 PM  
*    
*/
@Service
public class RightsServiceImpl implements RightsService{
    @Autowired
    SsoRightsDao ssoRightsDao;
    @Autowired
    SsoRoleRightsDao ssoRoleRightsDao;
    @Autowired
    RoleService roleService;
    
    @Override
    public void addRights(AddRightsReq req) {
        SsoRights entity = new SsoRights();
        entity.setGroupId(req.getGroupId());
        entity.setParentId(req.getParentId());
        entity.setRightIdentifier(req.getIdentifier());
        entity.setRightName(req.getRightName());
        entity.setIcon(req.getIcon());
        entity.setRightType(req.getRightType());
        ssoRightsDao.save(entity);
    }

    @Override
    public void delRights(DelRightsReq req) {
        SsoRights rights =  ssoRightsDao.getById(req.getRightsId());
        if (rights == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        int count = ssoRightsDao.queryChildCount(rights.getId());
        if (count > 0) {
            throw new BizException(CodeEnum.F_000001.getCode(), "父权限存在子权限，请先删除子权限");
        }
        rights.deleteById();
    }

    @Override
    public void updateRights(UpdateRightsReq req) {
        SsoRights rights =  ssoRightsDao.getById(req.getRightsId());
        if (rights == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        rights.setGroupId(req.getGroupId());
        rights.setParentId(req.getParentId());
        rights.setRightIdentifier(req.getIdentifier());
        rights.setRightName(req.getRightName());
        rights.setIcon(req.getIcon());
        rights.setRightType(req.getRightType());
        rights.updateById();
    }

    @Override
    public void setRights(SetRightsReq req) {
        SsoRoleRights query = new SsoRoleRights();
        query.setRoleId(req.getRoleId());
        QueryWrapper<SsoRoleRights> queryWrapper = new QueryWrapper<SsoRoleRights>(query);
        ssoRoleRightsDao.remove(queryWrapper);
        List<Long> rightsIds = req.getRightsIds();
        List<SsoRoleRights> entityList = new ArrayList<>();
        for (Long rightsId : rightsIds) {
            SsoRoleRights roleRights = new SsoRoleRights();
            roleRights.setRightsId(rightsId);
            roleRights.setRoleId(req.getRoleId());
            entityList.add(roleRights);
        }
        ssoRoleRightsDao.saveBatch(entityList);
    }

    @Override
    public List<RightsDto> roleRights(RoleRightsReq req) {
        List<SsoRights> rights = ssoRoleRightsDao.queryRoleRights(Arrays.asList(req.getRoleId()));
        List<RightsDto> dtos = new ArrayList<>();
        for (SsoRights ssoRights : rights) {
            RightsDto dto = new RightsDto();
            dto.setRightId(ssoRights.getId());
            dto.setGroupId(ssoRights.getGroupId());
            dto.setHavePermission(true);
            dto.setParentId(ssoRights.getParentId());
            dto.setRightIdentifier(ssoRights.getRightIdentifier());
            dto.setRightName(ssoRights.getRightName());
            dto.setIcon(ssoRights.getIcon());
            dto.setRightType(ssoRights.getRightType());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<RightsDto> userRights(UserRightsReq req) {
        String userId = SsoUserHolder.get().getUserId();
        List<RoleDto> roleDtos = roleService.queryUserRoles(userId);
        List<Long> roleIds = roleDtos.stream().filter(it->it.getGroupId().equals(req.getGroupId())).map(it->it.getRoleId()).collect(Collectors.toList());
        List<SsoRights> rights = new ArrayList<>();
        if (!CollectionUtils.isEmpty(roleIds)) {
            rights = ssoRoleRightsDao.queryRoleRights(roleIds);
        }
        List<SsoRights> ssoRights = ssoRoleRightsDao.queryUserRights(userId, req.getGroupId());
        List<Long> rightsIds = ssoRights.stream().map(it->it.getId()).collect(Collectors.toList());
        List<RightsDto> dtos = new ArrayList<>();
        for (SsoRights ssoRights2 : rights) {
            RightsDto dto = new RightsDto();
            dto.setRightId(ssoRights2.getId());
            dto.setGroupId(ssoRights2.getGroupId());
            dto.setHavePermission(rightsIds.contains(ssoRights2.getId()));
            dto.setParentId(ssoRights2.getParentId());
            dto.setRightIdentifier(ssoRights2.getRightIdentifier());
            dto.setRightName(ssoRights2.getRightName());
            dto.setIcon(ssoRights2.getIcon());
            dto.setRightType(ssoRights2.getRightType());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public Integer hasRights(HasRightsReq req) {
    	if (!ssoRightsDao.queryGroupHasRight(req.getGroupId(), req.getIdentifier())) {
    		return -1;
    	}
    	if (ssoRoleRightsDao.queryUserHaveRight(req.getUserId(), req.getGroupId(), req.getIdentifier())) {
    		return 1;
    	} else {
    		return 0;
    	}
    }

	@Override
	public List<RightsDto> groupRights(GroupRightsReq req) {
		List<SsoRights> rights = ssoRightsDao.queryGroupRight(req.getGroupId());
		List<RightsDto> dtos = new ArrayList<RightsDto>();
		for (SsoRights ssoRights : rights) {
            RightsDto dto = new RightsDto();
            dto.setRightId(ssoRights.getId());
            dto.setGroupId(ssoRights.getGroupId());
            dto.setParentId(ssoRights.getParentId());
            dto.setRightIdentifier(ssoRights.getRightIdentifier());
            dto.setRightName(ssoRights.getRightName());
            dto.setIcon(ssoRights.getIcon());
            dto.setRightType(ssoRights.getRightType());
            dtos.add(dto);
        }
		return dtos;
	}
}
